package com.hsmw.api.service;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hsmw.api.config.jwt.CurrentHsmwUserUtils;
import com.hsmw.api.config.jwt.LoginUser;
import com.hsmw.api.mapper.*;
import com.hsmw.common.base.BaseService;
import com.htn.common.core.constant.OtherNoticeTypeEnum;
import com.hsmw.api.utils.CommonUtils;
import com.hsmw.domain.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;

@Service
@Transactional(isolation = Isolation.REPEATABLE_READ, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class HsmwJobCollectionService extends BaseService<HsmwJobCollectionMapper, HsmwJobCollection> {


    @Autowired
    private HsmwJobCollectionMapper hsmwJobCollectionMapper;

    @Autowired
    private HsmwOrderMapper hsmwOrderMapper;

    @Autowired
    private HsmwUserMapper hsmwUserMapper;

    @Autowired
    private HsmwUserDriverMapper hsmwUserDriverMapper;


    @Autowired
    private HsmwPointsDetailsMapper hsmwPointsDetailsMapper;



    @Autowired
    private HsmwOtherNoticeInfoMapper hsmwOtherNoticeInfoMapper;



    @Autowired
    private CurrentHsmwUserUtils currentHsmwUserUtils;



    @Autowired
    private HsmwHtnUserMapper hsmwHtnUserMapper;


    @Autowired
    private HsmwForcemanItemMapper hsmwForcemanItemMapper;



    /**
     * 新增修改接口
     * @param hsmwJobCollection
     */
    public void jobConllectionSaveOrUpdate(HsmwJobCollection hsmwJobCollection) {
        LoginUser currentUser =currentHsmwUserUtils.getCurrentUserInfo();
        if(StringUtils.isNotBlank(hsmwJobCollection.getId())){
            hsmwJobCollectionMapper.updateById(hsmwJobCollection);

        }else {
            String  collectId = CommonUtils.generateId(hsmwJobCollection);
            hsmwJobCollection.setId(collectId);
            hsmwJobCollectionMapper.insert(hsmwJobCollection);

            HsmwPointsDetails hsmwPointsDetails = new HsmwPointsDetails();
            hsmwPointsDetails.setTypeIntegral(new BigDecimal(5));
            hsmwPointsDetails.setType("作业信息收集");
            hsmwPointsDetails.setDriverId(hsmwJobCollection.getDriId());
            hsmwPointsDetailsMapper.insert(hsmwPointsDetails);
            HsmwUserDriver hsmwUserDriver = hsmwUserDriverMapper.selectById(hsmwJobCollection.getDriId());
            long dri = Long.parseLong(hsmwUserDriver.getDriverIntegral());
            hsmwUserDriver.setDriverIntegral(String.valueOf(dri+5));
            hsmwUserDriverMapper.updateById(hsmwUserDriver);

            //存储其他通知信息。
            HsmwOtherNoticeInfo otherNoticeInfo = new HsmwOtherNoticeInfo();
            otherNoticeInfo.setName(currentUser.getName());
            otherNoticeInfo.setUserId(currentUser.getId());
            otherNoticeInfo.setReportId(collectId);
            otherNoticeInfo.setReadFlag(false);
            otherNoticeInfo.setMessage(hsmwJobCollection.getItemName());
            otherNoticeInfo.setReportType(OtherNoticeTypeEnum.WORK_COLLECT.getValue());
            hsmwOtherNoticeInfoMapper.insert(otherNoticeInfo);

        }
    }

    public List<HsmwHtnUser> findForManByOrderId(String orderId) {

        HsmwOrder hsmwOrder = hsmwOrderMapper.selectById(orderId);
        String itemId = hsmwOrder.getItemId();
        HashMap param = new HashMap();
        param.put("itemId",itemId);
        List<HsmwHtnUser> hsmwUsers = hsmwForcemanItemMapper.selectForemanListByItemId(param);
        return hsmwUsers;
    }

    public IPage<HsmwJobCollection> findJobCollectionList(IPage page, HsmwJobCollection hsmwJobCollection) {

        IPage<HsmwJobCollection> orderIPage = hsmwJobCollectionMapper.findJobCollectionList(page,hsmwJobCollection);
        return orderIPage;
    }
}
